package com.xmx.authcenter.service;

import cn.hutool.core.map.MapUtil;
import com.xmx.authcenter.dto.ClientDto;
import com.xmx.authcenter.feign.MemeberFeignClient;
import com.xmx.common.utils.ListFromEntity2VoUtils;
import com.xmx.common.utils.R;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class ClientDetailsServiceImpl implements ClientDetailsService {

    @Autowired
    private MemeberFeignClient memeberFeignClient;

    @Autowired
    PasswordEncoder passwordEncoder;


    @Override
    @SneakyThrows
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        try {
//          取出用户信息基本配置
            R clientDetailR = memeberFeignClient.getClientDetail(clientId);

            ClientDto clientDto = MapUtil.get(clientDetailR, "data", ClientDto.class);
                clientDto.setAuthorities((String) ((Map)clientDetailR.get("data")).get("authorizedGrantTypes"));
//            进行验证处理
            if (clientDto != null ) {

                BaseClientDetails clientDetails = new BaseClientDetails(
                        clientDto.getClientId(),
                        clientDto.getResouceIds(),
                        clientDto.getScopes(),
                        clientDto.getGrantType(),
                        clientDto.getAuthorities(),
                        clientDto.getRedirectUris()
                );
//                clientDetails.setClientSecret(clientDto.getClientSecret());
                clientDetails.setClientSecret(passwordEncoder.encode(clientDto.getClientSecret()));
                clientDetails.setAccessTokenValiditySeconds(clientDto.getAccessTokenValidity());
                clientDetails.setRefreshTokenValiditySeconds(clientDto.getRefreshTokenValidity());
                List<String> aa = new ArrayList<>();
                String[] split = ((String) ((Map) clientDetailR.get("data")).get("authorizedGrantTypes")).split(",");
                for (String s : split) {
                    aa.add(s);
                }
                clientDetails.setAuthorizedGrantTypes(aa);
                return clientDetails;
            } else {
                throw new NoSuchClientException("No client with requested id: " + clientId);
            }
        } catch (EmptyResultDataAccessException var4) {
            throw new NoSuchClientException("No client with requested id: " + clientId);
        }
    }
}
